package edu.hut.bookshop.controller;

import edu.hut.bookshop.pojo.Aftersale;
import edu.hut.bookshop.pojo.Order;
import edu.hut.bookshop.pojo.User;
import edu.hut.bookshop.service.AftersaleService;
import edu.hut.bookshop.util.ResultCode;
import edu.hut.bookshop.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/aftersale")
public class AfterController {

    @Autowired
    private AftersaleService aftersaleService;

    //显示售后表
    @GetMapping("/findbyuserid")
    public ResultVO findbyuseid(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Aftersale> aftersales = aftersaleService.selectAftbyuserId(user.getUserId());
        return new ResultVO(ResultCode.SUCCESS,aftersales);
    }

    //通过订单ID寻找售后
    @GetMapping("/findbyorderid")
    public ResultVO findbyorderid(Integer orderId){
        Aftersale aftersale = aftersaleService.selectAfterbyorderId(orderId);
        return new ResultVO(ResultCode.SUCCESS,aftersale);
    }

    //请求售后,改变状态
    @GetMapping("/updateafter")
    public ResultVO updateafter(Integer orderId,Integer pstatus){
        return new ResultVO(ResultCode.SUCCESS,aftersaleService.updatebyorderId(orderId,pstatus));
    }

    //改变名字
    @GetMapping("/updatejiajuname")
    public ResultVO updatjiajuname(Integer aftersaleId,String jiajuName){
        return new ResultVO(ResultCode.SUCCESS,aftersaleService.updatejiajuname(aftersaleId,jiajuName));
    }
    //删除售后
    @GetMapping("/deleteafter")
    public ResultVO deleteafter(Integer aftersaleId){
        return new ResultVO(ResultCode.SUCCESS,aftersaleService.deletebyaftersaleId(aftersaleId));
    }
    //搜索
    @GetMapping("/list")
    public ResultVO selectbyorderId(Integer orderId){
        Order order = aftersaleService.selectbyorderId(orderId);
        return new ResultVO(ResultCode.SUCCESS,order);
    }

}
